<?php
!defined('IN_ROOT') && exit( 'Access Denied' );
class ArticleController extends AdminModuleController 
{	
	public function actionAdmin() {
		$page = max(1, getgpc('page'));
		
		$sqladd="";
		$title = getgpc('title', 'R');
		if($title) {
			$sqladd = "title like '%$title%'";
		}
		
		$articles = $this->ArticleModel->findAll($sqladd, $page, GODHOUSE_PPP3);
		foreach($articles as &$v) {
			$category = $this->CategoryModel->findByPk($v['categoryId']);
			$v['categoryName'] = $category['cateName'];
			$v['createdTime2'] = date('Y-m-d H:i:s', $v['createdTime']);
		}
		$num = $this->ArticleModel->count($sqladd);
		$multipage = page($num, GODHOUSE_PPP3, $page, 'article-admin.htm?title='.$title);
		
		$this->view->assign('title', $title);
		$this->view->assign('multipage', $multipage);
		$this->view->assign('articles', $articles);
		
		$this->display('article_admin');
	}
	
	public function actionCreate() {
		if($article = getgpc('article', 'P')) {
			if(!$article['title']) {
				$this->message('标题不能为空。', 'article-create.htm');
			}
			$article['createdTime'] = $this->time;
			$upload = new Upload();
			$upfile = $upload->upfile($_FILES['picture'], 'upfile/article');
			$article['picture'] = $upfile;
			
			$this->ArticleModel->insert($article);
			$this->message('添加成功。', 'article-create.htm');
		}
		
//		$categorys = $this->CategoryModel->findAll();
		
		$this->view->assign('categorys', $this->_category());
		$this->view->assign('action', 'create');
		$this->view->assign('categoryList', $this->_category());
		$this->display('article_create');
	}
	
	public function actionUpdate() {
		$articleId = getgpc('articleId');
		$model = $this->ArticleModel->findByPk($articleId);
		$model['createdTime']	= date('Y-m-d',	$model['createdTime']) ;
		
		
		
		if($article = getgpc('article', 'P')) {
		$article['createdTime'] =	strtotime($article['createdTime']);
			if($_FILES['picture']['name']) {
				if($model['picture']) {
					@unlink(GODHOUSE_ROOT.substr($model['picture'], 1));
				}
				
				$upload = new Upload();
				$upfile = $upload->upfile($_FILES['picture'], 'upfile/article');
				$article['picture'] = $upfile;
				
				$article['createdTime'] = strtotime($_REQUEST['article[createdTime]']);
			}
			
			
			$this->ArticleModel->updateByPk($articleId, $article);
			
			$this->message('编辑成功。', 'article-update.htm?articleId='.$articleId);
		}
		
		$this->view->assign('categoryList', $this->_category());
//		$categorys = $this->CategoryModel->findAll();
		
		$this->view->assign('categorys', $categorys);
		$this->view->assign('article', $model);
		$this->view->assign('articleId', $articleId);
		$this->view->assign('action','update');
		$this->display('article_create');
	}
	
	public function actionDelete() {
		$articleId = getgpc('articleId');
		$this->ArticleModel->deleteByPk($articleId);
		$this->alert('删除成功。', GODHOUSE_DOMAIN_WWW.'admin/article-admin.htm');
	}
	
	function _category() {
		
		$categorys = $this->CategoryModel->findAll("`storeId`='2' AND parentId='0'");
		$r = array();
		foreach($categorys as $v) {
			$r[] = $v;
			$arr = $this->CategoryModel->findAll("parentId='{$v['categoryId']}'");
			foreach($arr as &$v2){
				$v2['cateName'] = "&nbsp;&nbsp;&nbsp;&nbsp;--".$v2['cateName'];
				$v2['storeId'] = Lookup::item('categoryModel',$v2['storeId']);
			}
			$r = array_merge($r, $arr);
		}
		return $r;
		
	}
	

}